package bb.lanxing.mvp.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import bb.lanxing.R;
import bb.lanxing.lib.widget.utils.Density;
import bb.lanxing.model.database.MapItem;

public class MapTileChooseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;
    List<MapItem> mapTileChooseBeans;
    OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int i);
    }

    public MapTileChooseAdapter(Context context, List<MapItem> list, boolean z) {
        this.context = context;
        this.mapTileChooseBeans = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        return new MapTileChooseViewHolder(LayoutInflater.from(this.context).inflate(R.layout.item_map_tile_choose, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        MapTileChooseViewHolder mapTileChooseViewHolder = (MapTileChooseViewHolder) viewHolder;
        final MapItem mapItem = this.mapTileChooseBeans.get(position);
        mapTileChooseViewHolder.mIvMapImg.setImageResource(mapItem.getMapImage());
        mapTileChooseViewHolder.mTvTileLabel.setText(mapItem.getName());
        mapTileChooseViewHolder.mTvTileLabel.setTextColor(this.context.getResources().getColor(mapItem.isSelected() ? R.color.global_blue_color : R.color.grey_999999));
        mapTileChooseViewHolder.mRlMapTile.setBackgroundResource(mapItem.isSelected() ? R.drawable.map_tile_item_selected_bg : R.drawable.map_tile_trans_item_bg);
        int dp2px = Density.dp2px(this.context, mapItem.isSelected() ? 1.0f : 0.5f);
        mapTileChooseViewHolder.mIvMapImg.setPadding(dp2px, dp2px, dp2px, dp2px);
        final int position1 = position;
        mapTileChooseViewHolder.mLlChooseMap.setOnClickListener(view -> {
            MapTileChooseAdapter.this.onItemClickListener.onItemClick(position1);
        });
    }

    @Override
    public int getItemCount() {
        return this.mapTileChooseBeans.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public class MapTileChooseViewHolder extends RecyclerView.ViewHolder {
        private ImageView mIvMapImg;
        private LinearLayout mLlChooseMap;
        private RelativeLayout mRlMapTile;
        private TextView mTvTileLabel;

        private void bindView(View view) {
            this.mLlChooseMap = view.findViewById(R.id.ll_choose_map);
            this.mRlMapTile = view.findViewById(R.id.rl_map_tile);
            this.mIvMapImg = view.findViewById(R.id.iv_map_img);
            this.mTvTileLabel = view.findViewById(R.id.tv_tile_label);
        }

        public MapTileChooseViewHolder(View view) {
            super(view);
            bindView(view);
        }
    }
}